package com.xasz.text2vec.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xasz.text2vec.dao.entity.KnowledgeBase;
import com.xasz.text2vec.dao.entity.Summary;
import com.xasz.text2vec.dto.SummaryDTO;
import com.xasz.text2vec.service.SummaryService;
import com.xasz.text2vec.utils.ToPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2023-06-29
 */
@RestController
@RequestMapping("/summary")
public class SummaryController {
    @Autowired
    SummaryService summaryService;
    /**
     * 新增项目
     *
     */
    @PutMapping("/addItem")
    public ResponseEntity<Boolean> addItem(@RequestBody SummaryDTO summaryDTO){
        //sysCode查重
        String sysCode = summaryDTO.getSysCode();
        Wrapper<Summary> queryWrapper=new QueryWrapper<Summary>().eq("sys_code",sysCode);
        if (summaryService.getOne(queryWrapper)!=null){
            return ResponseEntity.status(HttpStatus.CREATED).body(false);
        }
        return ResponseEntity.status(HttpStatus.CREATED).body(summaryService.save(ToPo.SummaryToPo(summaryDTO)));
    }

    /**
     * 删除项目
     *
     */
    @DeleteMapping("/deleteItem")
    public ResponseEntity<Boolean> deleteItem(@RequestParam(value = "id") int id){
        return ResponseEntity.status(HttpStatus.CREATED).body(summaryService.removeById(id));
    }
    /**
     * 查询列表
     */
    @GetMapping("/getItem")
    public Page<Summary> getItemList(@RequestParam(value = "pageNumber") int pageNumber,
                                     @RequestParam(value = "pageSize") int pageSize){
        Page<Summary> page = Page.of(pageNumber,pageSize);
        return summaryService.page(page);
    }

    /**
     * 修改项目
     */
    @PostMapping("/updateItem")
    public ResponseEntity<Boolean> updateItem(@RequestBody SummaryDTO summaryDTO){
        //sysCode查重
        String sysCode = summaryDTO.getSysCode();
        Wrapper<Summary> queryWrapper=new QueryWrapper<Summary>().eq("sys_code",sysCode);
        if (summaryService.getOne(queryWrapper)!=null){
            return ResponseEntity.status(HttpStatus.CREATED).body(false);
        }

        return ResponseEntity.status(HttpStatus.CREATED).body( summaryService.updateById(ToPo.SummaryToPo(summaryDTO)));
    }

}

